<template>
  <div class="container">
    <div class="login-form">
      <el-form ref="formData" :model="formData" >
        <el-form-item label="账号">
          <el-input v-model="formData.account" prefix-icon="el-icon-user"></el-input>
        </el-form-item>
        <el-form-item label="密码">
          <el-input type="password" v-model="formData.password" prefix-icon="el-icon-lock"></el-input>
        </el-form-item>
        <el-form-item v-if="active">
          <el-button  type="primary" class="btn-login" @click="login">登陆</el-button>
        </el-form-item>
        <el-form-item v-if="!active">
          <el-button type="primary" class="btn-login" @click="register">注册</el-button>
        </el-form-item>
      </el-form>
      <div class="form-footer">
        <span v-if="active">没有账户？<span class="btn-regist" @click="toRegist">注册</span></span>
        <span v-if="!active">已有账户？<span class="btn-regist" @click="toRegist">登陆</span></span>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
// const DEFAULT_FORM = JSON.parse(JSON.stringify(this.formData))
export default {
  name: 'Login',
  data () {
    return {
      active: '1',
      formData: {
        account: '',
        password: ''
      }
    }
  },
  methods: {
    toRegist () {
      this.active = !this.active
    },
    login () {
      this.$store.dispatch('Login', this.formData).then(res => {
        console.log(res)
        this.$notify.success('登陆成功')
        this.$router.push('/main')
        this.formData = {
          account: '',
          password: ''
        }
      }).catch(err => {
        console.log(err)
      })
    },
    register () {
      axios.post('/api/user/register', this.formData).then(res => {
        this.$notify({
          title: '成功',
          message: '注册成功',
          type: 'success'
        })
        this.active = true
        this.formData = {
          account: '',
          password: ''
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.container {
  width: 100%;
  background-image: url("../../static/images/bg.jpg");
  background-repeat: no-repeat;
  background-color: #b8e5f8;
  background-size: cover;
  width: 100%;
  height: 100vh;
  overflow: auto;
  .login-form {
    width: 25%;
    background: #fff;
    // padding: 10px;
    position: relative;
    top: 30%;
    box-shadow: 0 1px 3px rgba(26,26,26,.1);
    margin: 0 auto;
    .el-form {
      padding: 10px;
    }
    .btn-login {
      width: 100%;
    }
    .form-footer {
      width: 100%;
      background-color: #f6f6f6;
      height: 58px;
      padding: 10px;
      text-align: center;
      line-height: 45px;
      border-top: 1px solid #ebebeb;
      .btn-regist {
        color: blue;
        cursor: pointer;
      }
    }
  }
}

</style>
